<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveScaffold::AttributeParams</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveScaffold::AttributeParams</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/plugins/active_scaffold/lib/attribute_params_rb.html">
                vendor/plugins/active_scaffold/lib/attribute_params.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Provides support for param hashes assumed to be model attributes. Support
is primarily needed for creating/editing associated records using a nested
hash structure.
</p>
<p>
Paradigm Params Hash (should write unit tests on this):
</p>
<pre>
  params[:record] = {
    # a simple record attribute
    'name' =&gt; 'John',
    # a plural association hash
    'roles' =&gt; {
      # associate with an existing role
      '5' =&gt; {'id' =&gt; 5}
      # associate with an existing role and edit it
      '6' =&gt; {'id' =&gt; 6, 'name' =&gt; 'designer'}
      # create and associate a new role
      '124521' =&gt; {'name' =&gt; 'marketer'}
    }
    # a singular association hash
    'location' =&gt; {'id' =&gt; 12, 'city' =&gt; 'New York'}
  }
</pre>
<p>
Simpler association structures are also supported, like:
</p>
<pre>
  params[:record] = {
    # a simple record attribute
    'name' =&gt; 'John',
    # a plural association ... all ids refer to existing records
    'roles' =&gt; ['5', '6'],
    # a singular association ... all ids refer to existing records
    'location' =&gt; '12'
</pre>
<p>
}
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000160">attributes_hash_is_empty?</a>&nbsp;&nbsp;
      <a href="#M000159">find_or_create_for_params</a>&nbsp;&nbsp;
      <a href="#M000158">update_record_from_params</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000160" class="method-detail">
        <a name="M000160"></a>

        <div class="method-heading">
          <a href="#M000160" class="method-signature">
          <span class="method-name">attributes_hash_is_empty?</span><span class="method-args">(hash, klass)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Determines whether the given attributes hash is &quot;empty&quot;. This
isn&#8216;t a literal emptiness - it&#8216;s an attempt to discern whether
the user intended it to be empty or not.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000160-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000160-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/active_scaffold/lib/attribute_params.rb, line 163</span>
163:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">attributes_hash_is_empty?</span>(<span class="ruby-identifier">hash</span>, <span class="ruby-identifier">klass</span>)
164:       <span class="ruby-identifier">hash</span>.<span class="ruby-identifier">all?</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>,<span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
165:         <span class="ruby-comment cmt"># convert any possible multi-parameter attributes like 'created_at(5i)' to simply 'created_at'</span>
166:         <span class="ruby-identifier">column_name</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">'('</span>).<span class="ruby-identifier">first</span>
167:         <span class="ruby-identifier">column</span> = <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">columns_hash</span>[<span class="ruby-identifier">column_name</span>]
168: 
169:         <span class="ruby-comment cmt"># booleans and datetimes will always have a value. so we ignore them when checking whether the hash is empty.</span>
170:         <span class="ruby-comment cmt"># this could be a bad idea. but the current situation (excess record entry) seems worse.</span>
171:         <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">column</span> <span class="ruby-keyword kw">and</span> [<span class="ruby-identifier">:boolean</span>, <span class="ruby-identifier">:datetime</span>, <span class="ruby-identifier">:date</span>, <span class="ruby-identifier">:time</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">column</span>.<span class="ruby-identifier">type</span>)
172: 
173:         <span class="ruby-comment cmt"># defaults are pre-filled on the form. we can't use them to determine if the user intends a new row.</span>
174:         <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">column</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">default</span>.<span class="ruby-identifier">to_s</span>
175: 
176:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
177:           <span class="ruby-identifier">attributes_hash_is_empty?</span>(<span class="ruby-identifier">value</span>, <span class="ruby-identifier">klass</span>)
178:         <span class="ruby-keyword kw">else</span>
179:           <span class="ruby-identifier">value</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:empty?</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">false</span>
180:         <span class="ruby-keyword kw">end</span>
181:       <span class="ruby-keyword kw">end</span>
182:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000159" class="method-detail">
        <a name="M000159"></a>

        <div class="method-heading">
          <a href="#M000159" class="method-signature">
          <span class="method-name">find_or_create_for_params</span><span class="method-args">(params, klass, current)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Attempts to create or find an instance of klass (which must be an <a
href="../ActiveRecord.html">ActiveRecord</a> object) from the request
parameters given. If params[:id] exists it will attempt to find an existing
object otherwise it will build a new one.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000159-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000159-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/active_scaffold/lib/attribute_params.rb, line 142</span>
142:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">find_or_create_for_params</span>(<span class="ruby-identifier">params</span>, <span class="ruby-identifier">klass</span>, <span class="ruby-identifier">current</span>)
143:       <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attributes_hash_is_empty?</span>(<span class="ruby-identifier">params</span>, <span class="ruby-identifier">klass</span>)
144: 
145:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">params</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">:id</span>
146:         <span class="ruby-comment cmt"># modifying the current object of a singular association</span>
147:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">current</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">current</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">current</span>.<span class="ruby-identifier">id</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>]
148:           <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">current</span>
149:         <span class="ruby-comment cmt"># modifying one of the current objects in a plural association</span>
150:         <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">current</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">current</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:any?</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">current</span>.<span class="ruby-identifier">any?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">o</span><span class="ruby-operator">|</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">id</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>]}
151:           <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">current</span>.<span class="ruby-identifier">detect</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">o</span><span class="ruby-operator">|</span> <span class="ruby-identifier">o</span>.<span class="ruby-identifier">id</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>]}
152:         <span class="ruby-comment cmt"># attaching an existing but not-current object</span>
153:         <span class="ruby-keyword kw">else</span>
154:           <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>])
155:         <span class="ruby-keyword kw">end</span>
156:       <span class="ruby-keyword kw">else</span>
157:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">authorized_for?</span>(<span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">:create</span>)
158:       <span class="ruby-keyword kw">end</span>
159:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000158" class="method-detail">
        <a name="M000158"></a>

        <div class="method-heading">
          <a href="#M000158" class="method-signature">
          <span class="method-name">update_record_from_params</span><span class="method-args">(parent_record, columns, attributes)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Takes attributes (as from params[:record]) and applies them to the
parent_record. Also looks for association attributes and attempts to
instantiate them as associated objects.
</p>
<p>
This is a secure way to apply params to a record, because it&#8216;s based
on a loop over the columns set. The columns set will not yield unauthorized
columns, and it will not yield unregistered columns.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000158-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000158-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/active_scaffold/lib/attribute_params.rb, line 37</span>
 37:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">update_record_from_params</span>(<span class="ruby-identifier">parent_record</span>, <span class="ruby-identifier">columns</span>, <span class="ruby-identifier">attributes</span>)
 38:       <span class="ruby-identifier">action</span> = <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">new_record?</span> <span class="ruby-value">? </span><span class="ruby-operator">:</span><span class="ruby-identifier">create</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">:update</span>
 39:       <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">parent_record</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">authorized_for?</span>(<span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">action</span>)
 40: 
 41:       <span class="ruby-identifier">multi_parameter_attributes</span> = {}
 42:       <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
 43:         <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">include?</span> <span class="ruby-value str">'('</span>
 44:         <span class="ruby-identifier">column_name</span> = <span class="ruby-identifier">k</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">'('</span>).<span class="ruby-identifier">first</span>.<span class="ruby-identifier">to_sym</span>
 45:         <span class="ruby-identifier">multi_parameter_attributes</span>[<span class="ruby-identifier">column_name</span>] <span class="ruby-operator">||=</span> []
 46:         <span class="ruby-identifier">multi_parameter_attributes</span>[<span class="ruby-identifier">column_name</span>] <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span>]
 47:       <span class="ruby-keyword kw">end</span>
 48: 
 49:       <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">each</span> <span class="ruby-identifier">:for</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">parent_record</span>, <span class="ruby-identifier">:flatten</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">column</span><span class="ruby-operator">|</span>
 50:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">multi_parameter_attributes</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">name</span>
 51:           <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">:assign_multiparameter_attributes</span>, <span class="ruby-identifier">multi_parameter_attributes</span>[<span class="ruby-identifier">column</span>.<span class="ruby-identifier">name</span>])
 52:         <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">attributes</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">name</span>
 53:           <span class="ruby-identifier">value</span> = <span class="ruby-identifier">attributes</span>[<span class="ruby-identifier">column</span>.<span class="ruby-identifier">name</span>]
 54: 
 55:           <span class="ruby-comment cmt"># convert the value, possibly by instantiating associated objects</span>
 56:           <span class="ruby-identifier">value</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>)
 57:             <span class="ruby-comment cmt"># this is just for backwards compatibility. we should clean this up in 2.0.</span>
 58:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">form_ui</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:select</span>
 59:               <span class="ruby-identifier">ids</span> = <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">singular_association?</span>
 60:                 <span class="ruby-identifier">value</span>[<span class="ruby-identifier">:id</span>]
 61:               <span class="ruby-keyword kw">else</span>
 62:                 <span class="ruby-identifier">value</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">collect</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">hash</span><span class="ruby-operator">|</span> <span class="ruby-identifier">hash</span>[<span class="ruby-identifier">:id</span>]}
 63:               <span class="ruby-keyword kw">end</span>
 64:               (<span class="ruby-identifier">ids</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">ids</span>.<span class="ruby-identifier">empty?</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">ids</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
 65: 
 66:             <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">singular_association?</span>
 67:               <span class="ruby-identifier">hash</span> = <span class="ruby-identifier">value</span>
 68:               <span class="ruby-identifier">record</span> = <span class="ruby-identifier">find_or_create_for_params</span>(<span class="ruby-identifier">hash</span>, <span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>, <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{column.name}&quot;</span>))
 69:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">record</span>
 70:                 <span class="ruby-identifier">record_columns</span> = <span class="ruby-identifier">active_scaffold_config_for</span>(<span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>).<span class="ruby-identifier">subform</span>.<span class="ruby-identifier">columns</span>
 71:                 <span class="ruby-identifier">update_record_from_params</span>(<span class="ruby-identifier">record</span>, <span class="ruby-identifier">record_columns</span>, <span class="ruby-identifier">hash</span>)
 72:                 <span class="ruby-identifier">record</span>.<span class="ruby-identifier">unsaved</span> = <span class="ruby-keyword kw">true</span>
 73:               <span class="ruby-keyword kw">end</span>
 74:               <span class="ruby-identifier">record</span>
 75: 
 76:             <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">plural_association?</span>
 77:               <span class="ruby-identifier">collection</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">collect</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">key_value_pair</span><span class="ruby-operator">|</span>
 78:                 <span class="ruby-identifier">hash</span> = <span class="ruby-identifier">key_value_pair</span>[<span class="ruby-value">1</span>]
 79:                 <span class="ruby-identifier">record</span> = <span class="ruby-identifier">find_or_create_for_params</span>(<span class="ruby-identifier">hash</span>, <span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>, <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{column.name}&quot;</span>))
 80:                 <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">record</span>
 81:                   <span class="ruby-identifier">record_columns</span> = <span class="ruby-identifier">active_scaffold_config_for</span>(<span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>).<span class="ruby-identifier">subform</span>.<span class="ruby-identifier">columns</span>
 82:                   <span class="ruby-identifier">update_record_from_params</span>(<span class="ruby-identifier">record</span>, <span class="ruby-identifier">record_columns</span>, <span class="ruby-identifier">hash</span>)
 83:                   <span class="ruby-identifier">record</span>.<span class="ruby-identifier">unsaved</span> = <span class="ruby-keyword kw">true</span>
 84:                 <span class="ruby-keyword kw">end</span>
 85:                 <span class="ruby-identifier">record</span>
 86:               <span class="ruby-keyword kw">end</span>
 87:               <span class="ruby-identifier">collection</span>.<span class="ruby-identifier">compact</span>
 88:             <span class="ruby-keyword kw">end</span>
 89:           <span class="ruby-keyword kw">else</span>
 90:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">singular_association?</span>
 91:               <span class="ruby-comment cmt"># it's a single id</span>
 92:               <span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span>
 93:             <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">plural_association?</span>
 94:               <span class="ruby-comment cmt"># it's an array of ids</span>
 95:               <span class="ruby-identifier">column</span>.<span class="ruby-identifier">association</span>.<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span>
 96:             <span class="ruby-keyword kw">else</span>
 97:               <span class="ruby-comment cmt"># convert empty strings into nil. this works better with 'null =&gt; true' columns (and validations),</span>
 98:               <span class="ruby-comment cmt"># and 'null =&gt; false' columns should just convert back to an empty string.</span>
 99:               <span class="ruby-comment cmt"># ... but we can at least check the ConnectionAdapter::Column object to see if nulls are allowed</span>
100:               <span class="ruby-identifier">value</span> = <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">is_a?</span> <span class="ruby-constant">String</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-operator">!</span><span class="ruby-identifier">column</span>.<span class="ruby-identifier">column</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">column</span>.<span class="ruby-identifier">null</span>
101:               <span class="ruby-identifier">value</span>
102:             <span class="ruby-keyword kw">end</span>
103:           <span class="ruby-keyword kw">end</span>
104: 
105:           <span class="ruby-comment cmt"># we avoid assigning a value that already exists because otherwise has_one associations will break (AR bug in has_one_association.rb#replace)</span>
106:           <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{column.name}=&quot;</span>, <span class="ruby-identifier">value</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">through_association?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">column</span>.<span class="ruby-identifier">name</span>) <span class="ruby-operator">==</span> <span class="ruby-identifier">value</span>
107:           
108:           <span class="ruby-comment cmt"># Set any passthrough parameters that may be associated with this column (ie, file column &quot;keep&quot; and &quot;temp&quot; attributes)</span>
109:           <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">params</span>.<span class="ruby-identifier">empty?</span>
110:             <span class="ruby-identifier">column</span>.<span class="ruby-identifier">params</span>.<span class="ruby-identifier">each</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">p</span><span class="ruby-operator">|</span> <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{p}=&quot;</span>, <span class="ruby-identifier">attributes</span>[<span class="ruby-identifier">p</span>])}
111:           <span class="ruby-keyword kw">end</span>
112: 
113:         <span class="ruby-comment cmt"># plural associations may not actually appear in the params if all of the options have been unselected or cleared away.</span>
114:         <span class="ruby-comment cmt"># NOTE: the &quot;form_ui&quot; check isn't really necessary, except that without it we have problems</span>
115:         <span class="ruby-comment cmt"># with subforms. the UI cuts out deep associations, which means they're not present in the</span>
116:         <span class="ruby-comment cmt"># params even though they're in the columns list. the result is that associations were being</span>
117:         <span class="ruby-comment cmt"># emptied out way too often. BUT ... this means there's still a lingering bug in the default association</span>
118:         <span class="ruby-comment cmt"># form code: you can't delete the last association in the list.</span>
119:         <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">form_ui</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">plural_association?</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">column</span>.<span class="ruby-identifier">through_association?</span>
120:           <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{column.name}=&quot;</span>, [])
121:         <span class="ruby-keyword kw">end</span>
122:       <span class="ruby-keyword kw">end</span>
123: 
124:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">new_record?</span>
125:         <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">reflect_on_all_associations</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span>
126:           <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> [<span class="ruby-identifier">:has_one</span>, <span class="ruby-identifier">:has_many</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">a</span>.<span class="ruby-identifier">macro</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:through</span>]
127:           <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">association_proxy</span> = <span class="ruby-identifier">parent_record</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-node">&quot;@#{a.name}&quot;</span>)
128: 
129:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">ActiveScaffold</span><span class="ruby-operator">::</span><span class="ruby-constant">ReverseAssociationRequired</span>, <span class="ruby-value str">&quot;In order to support :has_one and :has_many where the parent record is new and the child record(s) validate the presence of the parent, ActiveScaffold requires the reverse association (the belongs_to).&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">reverse</span>
130: 
131:           <span class="ruby-identifier">association_proxy</span> = [<span class="ruby-identifier">association_proxy</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">macro</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:has_one</span>
132:           <span class="ruby-identifier">association_proxy</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">record</span><span class="ruby-operator">|</span> <span class="ruby-identifier">record</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{a.reverse}=&quot;</span>, <span class="ruby-identifier">parent_record</span>) }
133:         <span class="ruby-keyword kw">end</span>
134:       <span class="ruby-keyword kw">end</span>
135: 
136:       <span class="ruby-identifier">parent_record</span>
137:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>